package cn.iocoder.yudao.module.wecom.dal.mysql.customer;

import java.time.LocalDateTime;
import java.util.*;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.wecom.dal.dataobject.chatgroup.WeChatGroupDO;
import cn.iocoder.yudao.module.wecom.dal.dataobject.chatgroupmember.WeChatGroupMemberDO;
import cn.iocoder.yudao.module.wecom.dal.dataobject.customer.WeCustomerDO;
import cn.iocoder.yudao.module.wecom.dal.dataobject.msgrecord.WeMsgRecordDO;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.wecom.controller.admin.customer.vo.*;

/**
 * 企微客户 Mapper
 *
 * @author 数创万维
 */
@Mapper
public interface WeCustomerMapper extends BaseMapperX<WeCustomerDO> {

    default PageResult<WeCustomerDO> selectPage(WeCustomerPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<WeCustomerDO>()
                .eqIfPresent(WeCustomerDO::getParentId, reqVO.getParentId())
                .eqIfPresent(WeCustomerDO::getOwnerUserId, reqVO.getOwnerUserId())
                .likeIfPresent(WeCustomerDO::getName, reqVO.getName())
                .eqIfPresent(WeCustomerDO::getAvatar, reqVO.getAvatar())
                .likeIfPresent(WeCustomerDO::getCorpName, reqVO.getCorpName())
                .likeIfPresent(WeCustomerDO::getCorpFullName, reqVO.getCorpFullName())
                .likeIfPresent(WeCustomerDO::getPositionName, reqVO.getPositionName())
                .eqIfPresent(WeCustomerDO::getType, reqVO.getType())
                .eqIfPresent(WeCustomerDO::getGender, reqVO.getGender())
                .eqIfPresent(WeCustomerDO::getUnionId, reqVO.getUnionId())
                .eqIfPresent(WeCustomerDO::getExternalUserId, reqVO.getExternalUserId())
                .betweenIfPresent(WeCustomerDO::getCreateTime, reqVO.getCreateTime()));
//                .orderByDesc(WeCustomerDO::getLastChatTime));
    }
    default PageResult<WeCustomerRespVO> selectPage4(WeCustomerPageReqVO reqVO) {
        MPJLambdaWrapper<WeCustomerDO> wrapper = MPJWrappers.lambdaJoin(WeCustomerDO.class)
                .selectAll(WeCustomerDO.class)
                .selectAs("max(send_time)", WeCustomerRespVO::getLastChatTime)
                .leftJoin(WeMsgRecordDO.class, WeMsgRecordDO::getWeCustomerId, WeCustomerDO::getExternalUserId)
                .eq(WeCustomerDO::getOwnerUserId, reqVO.getOwnerUserId())
                .orderByDesc(WeMsgRecordDO::getSendTime)
                .groupBy(WeMsgRecordDO::getWeCustomerId)
                ;

        if (Objects.nonNull(reqVO.getLastChatTime())) {
            LocalDateTime[] lastChatTime = reqVO.getLastChatTime();
            wrapper.between(WeMsgRecordDO::getSendTime, lastChatTime[0], lastChatTime[1]);
        }

        if (Objects.nonNull(reqVO.getName())) {
            wrapper.like(WeCustomerDO::getName, reqVO.getName());
        }

        return selectJoinPage(reqVO, WeCustomerRespVO.class, wrapper);
    }
}